package com.syyo.gulimall.ware.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syyo.common.domain.ResultVo;
import com.syyo.common.enums.ResultEnum;
import com.syyo.common.exception.SysException;
import com.syyo.common.utils.MyJSONUtils;
import com.syyo.gulimall.ware.domain.entity.WareInfoEntity;
import com.syyo.gulimall.ware.domain.req.WareInfoReq;
import com.syyo.gulimall.ware.feign.MemberFeign;
import com.syyo.gulimall.ware.mapper.WareInfoMapper;
import com.syyo.gulimall.ware.service.WareInfoService;
import com.syyo.gulimall.ware.vo.FareVo;
import com.syyo.gulimall.ware.vo.MemberAddressVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;


/**
 * 仓库信息
 *
 * @author syyo
 * @email syyo@gmail.com
 * @date 2021-01-20 10:06:54
 */
@Service
public class WareInfoServiceImpl extends ServiceImpl<WareInfoMapper, WareInfoEntity> implements WareInfoService {

    @Autowired
    private MemberFeign memberFeign;

    @Autowired
    private WareInfoMapper wareInfoMapper;


    /**
     * 新增
     */
    @Override
    @Transactional
    public int add(WareInfoReq req) {
        WareInfoEntity entity = new WareInfoEntity();
        // 添加创建时间和更新时间
        BeanUtils.copyProperties(req, entity);
        int insert = wareInfoMapper.insert(entity);
        if (insert != 1) {
            throw new SysException(ResultEnum.E_10001.getCode(), ResultEnum.E_10001.getMessage());
        }
        return insert;
    }

    /**
     * 删除
     */
    @Override
    @Transactional
    public int del(Integer id) {
        int insert = wareInfoMapper.deleteById(id);
        if (insert != 1) {
            throw new SysException(ResultEnum.E_10002.getCode(), ResultEnum.E_10002.getMessage());
        }
        return insert;
    }

    /**
     * 编辑
     */
    @Override
    @Transactional
    public int edit(WareInfoReq req) {
        WareInfoEntity entity = new WareInfoEntity();
        // 添加更新时间
        BeanUtils.copyProperties(req, entity);
        int insert = wareInfoMapper.updateById(entity);
        if (insert != 1) {
            throw new SysException(ResultEnum.E_10003.getCode(), ResultEnum.E_10003.getMessage());
        }
        return insert;
    }

    /**
     * 详情
     */
    @Override
    public WareInfoEntity findOne(Integer id) {
        return wareInfoMapper.selectById(id);
    }

    /**
     * 列表
     */
    @Override
    public IPage<WareInfoEntity> findPage(Integer pageNum, Integer pageSize, WareInfoReq req) {
        Page<WareInfoEntity> teacherPage = new Page<WareInfoEntity>(pageNum, pageSize);
        QueryWrapper<WareInfoEntity> wrapper = new QueryWrapper<>();
        return wareInfoMapper.selectPage(teacherPage, wrapper);
    }

    /**
     * 列表
     */
    @Override
    public List<WareInfoEntity> findAll(WareInfoReq req) {
        QueryWrapper<WareInfoEntity> wrapper = new QueryWrapper<>();
        return wareInfoMapper.selectList(wrapper);
    }

    @Override
    public FareVo getFare(Long addrId) {
        FareVo fareVo = new FareVo();
        ResultVo info = memberFeign.findOne(addrId);
        Object data = info.getData();
        MemberAddressVo memberAddressVo = MyJSONUtils.typeCast(new TypeReference<MemberAddressVo>() {
        }, data);
        if (memberAddressVo != null) {
            String phone = memberAddressVo.getPhone();
            String substring = phone.substring(phone.length() - 1, phone.length());
            fareVo.setAddress(memberAddressVo);
            fareVo.setFare(new BigDecimal(substring));
            return fareVo;
        }
        return null;
    }
}